<?php

class Store 
{ 
    //private $Name; 
    private $Phone; 
    private $UserID;
    private $db;
    private $address ;
    private $ID ;
    private $name ;
    private $image ;
            
    function __construct($userID=null, $name=null, $location=null, $phone=null, $imageName=FALSE){
        //set data
     
      $this->setData($userID, $name, $location, $phone, $imageName);
        //connect db
      
       $this->connectDB();
       
     //insert to database
      if($userID==null) {
      //    die("you have to login first");
       }
       else{
                    
            $this->initStore();
           
       }
      
    }
    private function toArray() 
    { 
        return (array) $this; 
		
    }
    private function setData($userID,$name,$location,$phone,$imageName){
        $this->name=$name;
        $this->address=$location;
        $this->Phone=$phone;
     // $this->UploadPath=$uploadPath;
        $this->UserID=$userID;
       if($imageName!=false){
           
           
        include 'imageClass.php';
        $img = new imgUploader();
        $this->image=  $img->UploadPath($imageName);
		
    }
 else {
        $this->image=null;
    }
    }
    private function connectDB(){
        
      try {
          require_once'database.php';
     $db_config_file="db_config.php";
     $this->db=new database($db_config_file);
}   
       catch (Exception $ex) {
       $ex->getMessage(); 
}
  
    }
 
    private function initStore(){
 
    $sql="INSERT INTO store (storeName,address,stPhone,stImage,us_ID)
     VALUES ('$this->name','$this->address','$this->Phone','$this->image','$this->UserID')";
       
    if (!mysql_query($sql) )
{
         
      die("error:failed to create Store.");
} 
  else {
    
    $this->ID=$this->getID($this->UserID);
    $id1=$this->ID[0]['storeID'];
    $sql1="UPDATE  user SET  stID ='$id1' WHERE  userID ='$this->UserID'";
    if(!mysql_query($sql1))
    {
         
      die("error:failed to create Store1111.");
}  
    return true;
}
}
//////////////////////////////link 3shan yroo7 llstore 
    public function UpdateStore($storeID,$name,$location,$phone,$imageName){
         if($imageName!=false){
           
           
        include 'imageClass.php';
        $img = new imgUploader();
        $image=  $img->UploadPath($imageName);
		
    }
 else {
        $this->image=null;
    }
        $sql="UPDATE  store"
                . " SET  storeName='$name',address='$location',stPhone='$phone',stImage='$image'"
                . " WHERE  storeID ='$storeID'";
    if(!mysql_query($sql))
    {
         
        throw new Exception("error:failed to edit store.");
}  
    return true;
}
        
    
///////////////////////// query msh 8alt awy
function getStoreByID($id){
$sql="select * from store where storeID=$id";
$result=mysql_query($sql);
if($result){
$row[0] = mysql_fetch_array($result);
  if(count($row)>0)
     {
     if($row[0]['storeID']==$id)
               return $row;
     
     
     }
 
  }
  else{
     die("ERROR:can't get this store.");
    }
}
/////////////// 
/* 
function getStores(){
$sql="select * from store";
$result=mysql_query($sql);

while($row = mysql_fetch_array($result))
  {
    $this->displayStore($row);
  }
}*/
////modifed function
function  getStores(){
    $sql="select * from store";
    if(!$result=mysql_query($sql))
    {
        die("Error:can't get stores from database.");
    
    }
    else{
        $num=  mysql_num_rows($result);
        if($num > 0){
            
            for($i=0;$i<$num;$i++)
            {
               $row[$i] = mysql_fetch_array($result); 
            }            
         return $row;
            
            }
        
    }
    
    
   

}

//////////////////////////////////////
 public function getID($usID){
        $sql="select * from store where us_ID=$usID";
$result=mysql_query($sql);
if($result){
$row[0] = mysql_fetch_array($result);
  if(count($row)>0)
     {
     if($row[0]['us_ID']==$usID)
               return $row;
     
     
     }
 
  }
  else{
     die("ERROR:can't get this store.");
    }
        
    }
////////////////////////////////////////
	
} 

?>

